#!/usr/bin/perl
use Encode;

my $filepath = "./file/2005";
my $file = "$filepath/danci2.txt";
my $cquestion = "$filepath/cquestion.txt";
my $coption = "$filepath/coption.txt";
open my $in,"<","$file" or die "can't open $file";
open my $out_cq,">","$cquestion" or die "can't open $cquestion";
open my $out_co,">","$coption" or die "can't open $coption";


my $qid=8;
my $oid=20;
my $start = 0;
my @options;
my $otitle;
while (<$in>){
	chomp;
	$_ =~ s/^\s+//;
    $_ =~ s/\s+$//;
     if($_ =~ s/問\d+?．//){
     	$_ =~ s/^\s+//;
     	$qid++;
     	&printCQuestion($out_cq,$qid,$_);
    }else{
    	if($_ =~ s/（\d+?）．//){
    		@options = split(/\s+/,$_);
    		$otitle = shift @options;
    		$oid++;
    		&printCOption($out_co,$qid,$oid,1,$otitle,\@options);
    	}
    }
}
close $in;
close $out_cq;
close $out_co;


sub printCQuestion{
	my $out = shift;
	my $qid = shift;
	my $content = shift;
#QID	CONTENT	MEDIA	APPROVESTATUS	SECRETSTATUS	CHOICECNT	DIFFICULTY	IFHASANS	IFSTOP	CREATORID	CREATETIME
#1	私は<u>北区</u>にある学校に<u>通って</u>います。		2	1		5	1	0	1	20101214
	my @array = ($qid,$content,"",2,1,"",5,1,0,1,20101214);
	print $out join("\t",@array),"\n";
}
sub printCOption{
	my $out = shift;
	my $qid = shift;
	my $oid = shift;
	my $qtype = shift;
	my $otitle = shift;
	my $ref = shift;
	my @options = @$ref;
	foreach my $c (0..@options){		
		$options[$c] =~ s/.*?．//;
	}
#OPTIONID	OPTITLE	OPTYPE	OPTION1	OPTION2	OPTION3	OPTION4	OPTION5	OPTION6	OPTION7	OPTION8	OPTION9	ANSWER	ANSWER_DES	NCHOICES	IFSTOP	QID	IFEG	IFHASTITLE
#1	北区	1	きたく	きたぐ	きたくん	きたぐん								1	0	1	0	1
	my @array = ($oid,$otitle,$qtype,("")x11,1,0,$qid,0,1);
	@array[3..11]=@options;
	print $out join("\t",@array),"\n";
}